<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="../common/taglibs.jsp"%>
<%@ page import="java.util.*"%>
<%@ page import="com.opensymphony.xwork2.ActionSupport"%>
<%@ page import="net.aiform.account.pojo.*"%>
<%@ page import="net.aiform.bean.Constant"%>
<%@ page import="net.aiform.template.pojo.*"%>
<%@ page import="ext.cjt.*"%>
<%@ page import="net.aiform.template.action.*"%>
<script src="${ctx}/static/js/jquery.js" type="text/javascript"></script>
<script src="${ctx}/static/js/jquery.url.js" type="text/javascript"></script>
<script src="${ctx}/static/js/jquery.tablesorter.js"
	type="text/javascript"></script>
<script src="${ctx}/static/js/jquery.colorize.js" type="text/javascript"></script>
<script src="${ctx}/static/js/s.js" type="text/javascript"></script>
<link rel="stylesheet" rev="stylesheet" href="${ctx}/static/css/s.css"
	type="text/css" media="all" />
<body onload="listRecords()">
<div id="list_results"></div>
<div id="testresult"></div>
</body>
<script type="text/javascript">
	if (window.af == undefined)
		window.af = [];
	if (af.record == undefined)
		af.record = [];
	if (af.record.view == undefined)
		af.record.view = [];
	function listRecords() {
		var code = jQuery.url.attr("file");
		$.ajax( {
			url : "${ctx}/a/r/list/t/" + code,
			type : "post",
			dataType : "json",
			timeout : 5000,
			error : function() {
				alert("Error loading XML document");
			},
			success : function(root) {
				var json = root.json;
				af.record.view.listRecordsByTemplateCallback(json);
			}
		});
	}
	af.record.view.listRecordsByTemplateCallback = function(message) {
		if (null == message.data)
			return;
		var resultTable = $("#list_results");
		resultTable.append('<table id="template-result-table-list"></table>');
		af.record.view.initTemplateResultsContainer(resultTable.children()
				.eq(0), message.data);
		af.record.view.listResultsByTemplate(resultTable.children().eq(0),
				message.data);
		$("#template-result-table-list").tablesorter();
		$("#template-result-table-list").colorize( {
			ignoreHeaders : true
		});
	};
	af.record.view.initTemplateResultsContainer = function(c, r) {
		var t = r.template;
		var fs = t.fields;
		c.append('<thead><tr></tr></thead>');
		c = c.children().eq(0);
		var row = c.children().eq(0);
		af.record.view.initTemplateHeader(row, fs);
	};
	af.record.view.initTemplateHeader = function(row, fields) {
		row.append('<th></th>');
		for ( var i = 1; i < fields.length + 1; i++) {
			for ( var j = 0; j < fields.length; j++) {
				var f = fields[j];
				var title = f.name;
				var position = f.position;
				if (i == position) {
					if (i == fields.length) {
						row.append('<th>' + title + '</th>');
					} else {
						row.append('<th>' + title + '</th>');
					}
				}
			}
		}
	};
	af.record.view.initTemplateRecord = function(c, template, record) {
		var fs = template.fields;
		var vs = record.values;
		var tr = $('<tr></tr>');
		c.append(tr);
		tr
				.append('<td><input type="checkbox" name="' + record.record + '"></td>');
		for ( var n = 1; n < fs.length + 1; n++) {
			for ( var i = 0; i < fs.length; i++) {
				var f = fs[i];
				var id = f.id;
				var position = f.position;
				if (n == position) {
					for ( var j = 0; j < vs.length; j++) {
						var v = vs[j];
						var field = v.field;
						var value = v.value;
						if (id == field) {
							tr.append('<td>' + value + '</td>');
						}
					}
				}
			}
		}
	};
	af.record.view.listResultsByTemplate = function(c, r) {
		var records = r.records;
		var template = r.template;
		for ( var i = 0; i < records.length; i++) {
			var record = records[i];
			af.record.view.initTemplateRecord(c, template, record);
		}
	};
</script>